package com.offcn.scwproject.controller;

import com.alibaba.fastjson.JSON;
import com.offcn.dycommon.enums.ProjectStatusEnume;
import com.offcn.dycommon.response.AppResponse;
import com.offcn.scwproject.contants.ProjectContants;
import com.offcn.scwproject.pojo.TReturn;
import com.offcn.scwproject.service.ProjectCreateService;
import com.offcn.scwproject.vo.req.ProjectBaseInfoVo;
import com.offcn.scwproject.vo.req.ProjectRedisStorageVo;
import com.offcn.scwproject.vo.req.ProjectReturnVo;
import com.offcn.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/project")
@Slf4j
@Api(tags = "项目的创建--初始化")
public class ProjectCreateController {
    @Autowired
    private ProjectCreateService projectCreateService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation("项目初始化的第一步：")
@PostMapping("/init")
    public AppResponse init(BaseVo vo){
        String accessToken = vo.getAccessToken();
        String memberId = redisTemplate.opsForValue().get(accessToken);
        if(StringUtils.isEmpty(memberId)){//null ""
            return AppResponse.fail("请登录");
        }
        //初始化项目
        int id = Integer.parseInt(memberId);
        String s = projectCreateService.initCreateProject(id);
      return AppResponse.ok(s);
    }
     @ApiOperation("第二步 保存页面的基本信息")
     @PostMapping("/savebaseInfo")
    public AppResponse saveBaseInfo(ProjectBaseInfoVo vo){
        //从redis获取项目
       String redisProject =     redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX+vo.getProjectToken());
        //转化成对象类型
         ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(redisProject, ProjectRedisStorageVo.class);
         //把vo--->存入缓存对象
         BeanUtils.copyProperties(vo,projectRedisStorageVo);
         //转换成json字符串
         String s = JSON.toJSONString(projectRedisStorageVo);
         //存入redis
         redisTemplate.opsForValue().set(ProjectContants.TEMP_PROJECT_PREFIX+vo.getProjectToken(),s);
         return AppResponse.ok("ok");
     }
     //第三步，保存回报
    @ApiOperation("项目初始化第三步，保存回报")
    @PostMapping("/savereturn")
    public AppResponse saveReturnInfo(@RequestBody List<ProjectReturnVo> pro){
        //1.拿项目令牌
        String projectToken = pro.get(0).getProjectToken();
        //2.从redis取出临时对象
        String redisProject = redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX + projectToken);
        //3.redisProject-->对象
        ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(redisProject, ProjectRedisStorageVo.class);
        //4.前端的回报保存到集合里
        List list = new ArrayList();
        for (ProjectReturnVo projectReturnVo : pro) {
            TReturn tReturn = new TReturn();
            BeanUtils.copyProperties(projectReturnVo,tReturn);
            list.add(tReturn);
        }
        //5.把集合存入临时对象
        projectRedisStorageVo.setProjectReturns(list);
        //6.存入redis
        String s = JSON.toJSONString(projectRedisStorageVo);
        //存入redis
        redisTemplate.opsForValue().set(ProjectContants.TEMP_PROJECT_PREFIX+projectToken,s);
        return AppResponse.ok("ok");

    }
    //保存项目到数据库
    @ApiOperation("项目发起第4步-项目保存项目回报信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken",value = "用户令牌",required = true),
            @ApiImplicitParam(name = "projectToken",value="项目标识",required = true),
            @ApiImplicitParam(name="ops",value="用户操作类型 0-保存草稿 1-提交审核",required = true)})
    @PostMapping("/submit")
    public AppResponse saveProjectInfo(String accessToken,String projectToken,String ops){
        String memberId = redisTemplate.opsForValue().get(accessToken);
        if(memberId.isEmpty()){
            return AppResponse.fail("请登录");
        }
        //获取项目
        String redisProject = redisTemplate.opsForValue().get(ProjectContants.TEMP_PROJECT_PREFIX + projectToken);
        if(StringUtils.isEmpty(redisProject)){
             return AppResponse.fail("没有此项目");

        }
        ProjectRedisStorageVo projectRedisStorageVo = JSON.parseObject(redisProject, ProjectRedisStorageVo.class);
        //保存项目
        if(ops.equals("1")){
            projectCreateService.saveProjectInfo(ProjectStatusEnume.SUBMIT_AUTH,projectRedisStorageVo);
           return AppResponse.ok("提交审核成功");
        }else if(ops.equals("0")){
            //0草稿----0即将开始
            projectCreateService.saveProjectInfo(ProjectStatusEnume.DRAFT,projectRedisStorageVo);
            return AppResponse.ok("已经存入草稿");

        }else {
            return AppResponse.fail("不允许的操作");
        }

    }
}
